/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.chili;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQChiliUtils
{

	/**
	 * Create a JQChiliBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQChiliBuilder
	 */
	public static JQChiliBuilder create(String id, String key)
	{
		return new JQChiliBuilder(new JQChili(id, (key == null ? JQChiliPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQChiliBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQChiliBuilder
	 */
	public static JQChiliBuilder create(String id)
	{
		return new JQChiliBuilder(new JQChili(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQChiliBuilder
	{
		/** JQuery component */
		private JQChili component;
		/** JQuery default options */
		private JQChiliOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQChiliBuilder(JQChili component)
		{
			this.component = component;
			this.options = new JQChiliOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		};

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		};

		/**
		 * Sets the automatic option value
		 * 
		 * @param automatic
		 *            chili automatic
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder automatic(boolean automatic)
		{
			options.setAutomatic(automatic);
			return this;
		}

		/**
		 * Sets the lineNumbers option value
		 * 
		 * @param lineNumbers
		 *            chili lineNumbers
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder lineNumbers(boolean lineNumbers)
		{
			options.setLineNumbers(lineNumbers);
			return this;
		}

		/**
		 * Sets the recipeLoading option value
		 * 
		 * @param recipeLoading
		 *            chili recipeLoading
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder recipeLoading(boolean recipeLoading)
		{
			options.setRecipeLoading(recipeLoading);
			return this;
		}

		/**
		 * Sets the automaticSelector option value
		 * 
		 * @param automaticSelector
		 *            chili automaticSelector
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder automaticSelector(String automaticSelector)
		{
			options.setAutomaticSelector(automaticSelector);
			return this;
		}

		/**
		 * Sets the codeLanguage option value
		 * 
		 * @param codeLanguage
		 *            chili codeLanguage
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder codeLanguage(String codeLanguage)
		{
			options.setCodeLanguage(codeLanguage);
			return this;
		}

		/**
		 * Sets the recipeFolder option value
		 * 
		 * @param recipeFolder
		 *            chili recipeFolder
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder recipeFolder(String recipeFolder)
		{
			options.setRecipeFolder(recipeFolder);
			return this;
		}

		/**
		 * Sets the replaceSpace option value
		 * 
		 * @param replaceSpace
		 *            chili replaceSpace
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder replaceSpace(String replaceSpace)
		{
			options.setReplaceSpace(replaceSpace);
			return this;
		}

		/**
		 * Sets the replaceTab option value
		 * 
		 * @param replaceTab
		 *            chili replaceTab
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder replaceTab(String replaceTab)
		{
			options.setReplaceTab(replaceTab);
			return this;
		}

		/**
		 * Sets the replaceNewLine option value
		 * 
		 * @param replaceNewLine
		 *            chili replaceNewLine
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder replaceNewLine(String replaceNewLine)
		{
			options.setReplaceNewline(replaceNewLine);
			return this;
		}

		/**
		 * Sets the selectionStyle option value
		 * 
		 * @param selectionStyle
		 *            chili selectionStyle
		 * @return JQChiliBuilder
		 */
		public JQChiliBuilder selectionStyle(String selectionStyle)
		{
			options.setSelectionStyle(selectionStyle);
			return this;
		}

		/**
		 * @return JQChili
		 */
		public JQChili build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}